package com.jialanmo.helper.recyclerview;

import android.view.ViewGroup;

import java.util.List;

public final class HeaderAndFooterAdapter<T> extends BaseAdapter<T> {

    private final static int VIEWTYPE_HEADER = -1;
    private final static int VIEWTYPE_FOOTER = -2;

    private final BaseAdapter<T> mOriginalAdapter;

    private OnViewCreateListener mHeaderViewCreateListener;
    private OnViewCreateListener mFooterViewCreateListener;

    public HeaderAndFooterAdapter(BaseAdapter<T> adapter) {
        mOriginalAdapter = adapter;
    }

    @Override
    public List<T> getData() {
        return mOriginalAdapter.getData();
    }

    @Override
    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener, int offset) {
        mOriginalAdapter.setOnLoadMoreListener(onLoadMoreListener, offset);
    }

    OnViewCreateListener getHeaderViewCreateListener() {
        return mHeaderViewCreateListener;
    }

    public void setHeaderView(OnViewCreateListener headerViewCreateListener) {
        if (mHeaderViewCreateListener == headerViewCreateListener) {
            return;
        }
        if (mHeaderViewCreateListener == null) {
            mHeaderViewCreateListener = headerViewCreateListener;
            notifyItemInserted(0);
            return;
        }
        if (headerViewCreateListener == null) {
            mHeaderViewCreateListener = null;
            notifyItemRemoved(0);
            return;
        }
        mHeaderViewCreateListener = headerViewCreateListener;
        notifyItemChanged(0);
    }

    OnViewCreateListener getFooterViewCreateListener() {
        return mFooterViewCreateListener;
    }

    public void setFooterView(OnViewCreateListener footerViewCreateListener) {
        if (mFooterViewCreateListener == footerViewCreateListener) {
            return;
        }
        if (mFooterViewCreateListener == null) {
            mFooterViewCreateListener = footerViewCreateListener;
            notifyItemInserted(getItemCount() - 1);
        }
        if (footerViewCreateListener == null) {
            mFooterViewCreateListener = null;
            notifyItemRemoved(getItemCount());
        }
        mFooterViewCreateListener = footerViewCreateListener;
        notifyItemChanged(getItemCount() - 1);
    }

    boolean hasHeaderView() {
        return mHeaderViewCreateListener != null;
    }

    boolean hasFooterView() {
        return mFooterViewCreateListener != null;
    }

    boolean isHeaderView(int position) {
        return hasHeaderView() && position == 0;
    }

    boolean isFooterView(int position) {
        return hasFooterView() && position == getItemCount() - 1;
    }

    int getDataCount() {
        return mOriginalAdapter.getItemCount();
    }

    @Override
    public int getItemCount() {
        int count = getDataCount();
        if (hasHeaderView()) {
            count++;
        }
        if (hasFooterView()) {
            count++;
        }
        return count;
    }

    @Override
    public int getItemViewType(int position) {
        if (isHeaderView(position)) {
            return VIEWTYPE_HEADER;
        }
        if (isFooterView(position)) {
            return VIEWTYPE_FOOTER;
        }
        return hasHeaderView() ? mOriginalAdapter.getItemViewType(position - 1) : mOriginalAdapter.getItemViewType(position);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == VIEWTYPE_HEADER) {
            return new ViewHolder(mHeaderViewCreateListener.onViewCreate(parent), this, null);
        }
        if (viewType == VIEWTYPE_FOOTER) {
            return new ViewHolder(mFooterViewCreateListener.onViewCreate(parent), this, null);
        }
        return new ViewHolder(mOriginalAdapter.onCreateViewHolder(parent, viewType), this);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (isHeaderView(position) || isFooterView(position)) {
            return;
        }
        mOriginalAdapter.onBindViewHolder(holder, hasHeaderView() ? position - 1 : position);
    }

    @Override
    public long getItemId(int position) {
        return hasHeaderView() ? mOriginalAdapter.getItemId(position - 1) : getItemId(position);
    }
}
